package com.wealth.scsj.p_app.view.activity;

import android.support.design.widget.AppBarLayout;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.adapter.SelectMarkAdapter;
import com.wealth.scsj.p_app.utils.AAMyAlertDialog;
import com.wealth.scsj.p_app.utils.DialogUtils;
import com.wealth.scsj.p_app.utils.SkipUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *  账单记录
 */

public class BillingRecordsActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private TextView tv_right;
    private SelectMarkAdapter selectMarkAdapter;
    private SelectMarkAdapter selectMarkAdapter1;
    private RecyclerView rv_select_mark;
    private RecyclerView rv_select_mark1;
    private List<String> mDatas;
    private List<String> mDatas1;
    private AppBarLayout titleBarLayout;
    private PopupWindow mPopupWindow;
    private int sortposion = -1;//记录筛选优惠活动上次点击的位置 -1 表示空

    private boolean isPopShowing = false;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_billing_records);
        initToolbar();
        setToolbarTitle("账单记录");
        hasWhiteBack(true);

        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.toolbar_title);
        tv_right = findViewById(R.id.toolbar_title_right);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("筛选");

        titleBarLayout = findViewById(R.id.title_layout);

    }

    @Override
    protected void initListener() {
        tv_right.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        /**
         * 筛选的弹窗数据
         */
        mDatas = new ArrayList<>();
        mDatas.add("支出");
        mDatas.add("收入");

        mDatas1 = new ArrayList<>();
        mDatas1.add("新用户优惠");
        mDatas1.add("满减活动");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_title_right:
                //点击筛选按钮
                if (!isPopShowing) {
                    showSelectPopWindow();
                } else {
                    mPopupWindow.dismiss();
                    isPopShowing = false;
                }
                break;
        }
    }

    private void showSelectPopWindow() {

        //设置背景半透明
        // backgroundAlpha(0.5f);
        View mlayoutPay = View.inflate(this, R.layout.billing_recorde_select_layout, null);
        mPopupWindow = new PopupWindow(mlayoutPay, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

        rv_select_mark = mlayoutPay.findViewById(R.id.rv_select_mark);
        rv_select_mark1 = mlayoutPay.findViewById(R.id.rv_select_mark1);
        rv_select_mark.setLayoutManager(new GridLayoutManager(this, 3));
        rv_select_mark1.setLayoutManager(new GridLayoutManager(this, 3));

        selectMarkAdapter = new SelectMarkAdapter(this, mDatas, sortposion);
        selectMarkAdapter1 = new SelectMarkAdapter(this, mDatas1, sortposion);
        rv_select_mark.setAdapter(selectMarkAdapter);
        rv_select_mark1.setAdapter(selectMarkAdapter1);
        mPopupWindow.showAsDropDown(titleBarLayout);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOnDismissListener(new BillingRecordsActivity.popupDismissListener());

        isPopShowing = true;

        TextView cancelBtn = mlayoutPay.findViewById(R.id.cancel_btn);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 暂时弹出dialog
                final AAMyAlertDialog alertDialog = new AAMyAlertDialog(BillingRecordsActivity.this);
                alertDialog.setMessage("注销实名认证");
                alertDialog.setMessageRed("注意：平台绑定的银行卡将被解绑");

                alertDialog.setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            alertDialog.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                alertDialog.setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            // alertDialog.dismiss();
                            //TODO 暂时弹出dialog

                            DialogUtils.getInstance()
                                    .with(BillingRecordsActivity.this)//上下文
                                    .setlayoutId(R.layout.confirm_arrived_pop_layout)//布局文件
                                    .setlayoutPosition(Gravity.CENTER)//位置
                                    .setlayoutAnimaType(1)//动画类型:0自下而上动画，1淡入淡出动画
                                    .setlayoutPading(20, 0, 20, 20)//与屏幕距离
                                    .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                                        @Override
                                        public void getChildView(View view, int layoutResId) {
                                            //处理dialog内部控件的点击事件
                                            RelativeLayout cancelBtn = view.findViewById(R.id.cancel_btn);
                                            cancelBtn.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    //TODO 暂时弹出dialog
                                                    DialogUtils.getInstance()
                                                            .with(BillingRecordsActivity.this)//上下文
                                                            .setlayoutId(R.layout.tel_to_comsumer_layout)//布局文件
                                                            .setlayoutPosition(Gravity.CENTER)//位置
                                                            .setlayoutAnimaType(1)//动画类型:0自下而上动画，1淡入淡出动画
                                                            .setlayoutPading(150, 0, 150, 20)//与屏幕距离
                                                            .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                                                                @Override
                                                                public void getChildView(View view, int layoutResId) {
                                                                    //处理dialog内部控件的点击事件
                                                                    TextView makingCallBtn = view.findViewById(R.id.making_call_btn);
                                                                    makingCallBtn.setOnClickListener(new View.OnClickListener() {
                                                                        @Override
                                                                        public void onClick(View view) {
                                                                            //TODO 暂时弹出dialog

                                                                            DialogUtils.getInstance()
                                                                                    .with(BillingRecordsActivity.this)//上下文
                                                                                    .setlayoutId(R.layout.making_call_layout)//布局文件
                                                                                    .setlayoutPosition(Gravity.CENTER)//位置
                                                                                    .setlayoutAnimaType(1)//动画类型:0自下而上动画，1淡入淡出动画
                                                                                    .setlayoutPading(100, 0, 100, 20)//与屏幕距离
                                                                                    .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                                                                                        @Override
                                                                                        public void getChildView(View view, int layoutResId) {
                                                                                            //处理dialog内部控件的点击事件
                                                                                            TextView callNoticeTv = view.findViewById(R.id.call_notice_tv);
//                                                                                            makingCallBtn.setOnClickListener(new View.OnClickListener() {
//                                                                                                @Override
//                                                                                                public void onClick(View view) {
//                                                                                                    //TODO 暂时弹出dialog
//
//
//
//                                                                                                }
//                                                                                            });

                                                                                        }
                                                                                    })
                                                                                    .show();

                                                                        }
                                                                    });

                                                                }
                                                            })
                                                            .show();
                                                }
                                            });

                                        }
                                    })
                                    .show();


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                alertDialog.show();
            }
        });

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        this.getWindow().setAttributes(lp);
    }

    /**
     * 将背景透明度改回来
     */
    class popupDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }
}
